import java.util.Objects;

public class Demo1 {

    public static int count = 0;
    public static void main(String[] args) throws InterruptedException {
        //先创建一个对象,使用这个对象来作为锁
        Object locker = new Object();
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                synchronized (locker){//同步,这里指的是互斥
                    count++;
                }
            }
        });
        //锁先被t1占领,t2先进入阻塞等待,只有当t1进行完毕之后,t2才能占领锁
        //这样t2的load就在t1的save之后  但是两个线程都是并发执行的
        Thread t2 = new Thread(()->{
            for (int i = 0; i <50000 ; i++) {
                synchronized (locker){
                    count++;
                }
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println("count = "+count);
    }
}
